﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Windows.Forms;
using System.Reflection;

namespace ProgNetComponents.Controls
{
    /// <summary>
    /// Dialog to pickup directory
    /// </summary>
    public class RootFolderBrowserDialog : Component
    {
        /// <summary>
        /// Creates RootFolderBrowserDialog instance
        /// </summary>
        public RootFolderBrowserDialog()
        {
            RootName = "%ROOT%";
            ReturnType = PathType.PathWithShortRoot;
        }
        
        /// <summary>
        /// Gets or sets Root directory
        /// </summary>
        public string RootPath
        {
            get;
            set;
        }
        /// <summary>
        /// Gets or sets dialogs tittle
        /// </summary>
        public string Title
        {
            get;
            set;
        }
        /// <summary>
        /// Gets or sets short name for RootPath
        /// </summary>
        public string RootName
        {
            get;
            set;
        }
        /// <summary>
        /// Specifies how path should be returned
        /// </summary>
        public PathType ReturnType
        {
            get;
            set;
        }
        /// <summary>
        /// Gets or sets path selected
        /// </summary>
        public string Path
        {
            get;
            set;
        }
        /// <summary>
        /// Shows BrowseForFolder dialog
        /// </summary>
        /// <returns></returns>
        public DialogResult ShowDialog()
        {
            DialogResult dlg = DialogResult.None;
            try
            {
                var shellType = Type.GetTypeFromProgID("Shell.Application");
                var shell = Activator.CreateInstance(shellType);
                if (Path.IsNotNull())
                {
                    try
                    {
                        System.IO.Directory.SetCurrentDirectory(Path);
                    }
                    catch { }
                }
                var folder = shellType.InvokeMember("BrowseForFolder",
                BindingFlags.InvokeMethod, null, shell, new object[] { 0, Title, 0, RootPath });
                if (folder == null)
                {
                    return DialogResult.Cancel;
                }
                var folderSelf = folder.GetType().InvokeMember("Self", BindingFlags.GetProperty, null, folder, null);
                string folderName=  folderSelf.GetType().InvokeMember("Path", BindingFlags.GetProperty, null, folderSelf, null) as string;
                if (folderName.IsNotNull())
                {
                    switch (ReturnType)
                    {
                        case PathType.FullPath:
                            Path = folderName;
                            break;
                        case PathType.PathWithNoRoot:
                            Path = folderName.XReplace("^" + RootPath.TrimEnd('\\').XEscape(), "");
                            break;
                        case PathType.PathWithShortRoot:
                            Path = RootName + "\\" + folderName.XReplace("^" + RootPath.TrimEnd('\\').XEscape(), "");
                            break;
                    }
                    dlg = DialogResult.OK;
                }
            }
            catch { dlg = DialogResult.Abort; }
            return dlg;
        }

    }
}
